package com.pdstudio.util;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.reflect.TypeToken;
import com.pdstudio.bean.ResponseData;
import com.pdstudio.bean.SimpleCity;
import com.pdstudio.bean.WeatherFine;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 所有的输入信息都保存在这里
 * */
public class SharePreferenceUtil {
	public static final String CITY_SHAREPRE_FILE = "city";
	private static final String CASH_CITY = "_city";
	private static final String SIMPLE_CLIMATE = "simple_climate";
	private static final String SIMPLE_TEMP = "simple_temp";
	private static final String TIMESAMP = "timesamp";
	private static final String TIME = "time";
	private static final String PUSH_TIME = "push_time";// 推送时间
	private static final String BROADCAST_TIME = "broadcast_time";// 播报时间
	private static final String ISPUSH = "is_push";// 是否推送信息
	private static final String ISBROADCAST = "is_broadcast";// 是否开启播报
	private static final String AUTHCODE = "authcode";// 授权码
	private static final String WEATHERSTATUS="weatherstatus";//天气情况
	private static final String WEATHERBGRES="bgresid";//天气背景
	private static final String FOCUSCITYS="focuscitys";//关注的城市
	private SharedPreferences sp;
	private SharedPreferences.Editor editor;

	public SharePreferenceUtil(Context context, String file) {
		sp = context.getSharedPreferences(file, Context.MODE_PRIVATE);
		editor = sp.edit();
	}
	public void setWeatherStatus(String weatherStatus){
		editor.putString(WEATHERSTATUS, weatherStatus);
		editor.commit();
	}
	public String getWeatherStatus(){
		return sp.getString(WEATHERSTATUS, "晴");
	}	

	/**
	 * 授权码
	 * */
	public void setAuthCode(String authCode) {
		editor.putString(AUTHCODE, authCode);
		editor.commit();
	}

	public String getAuthCode() {
		return sp.getString(AUTHCODE, "");
	}

	// 设置--是否播报
	public void setIsBroadcast(boolean isBroadcast) {
		editor.putBoolean(ISBROADCAST, isBroadcast);
		editor.commit();
	}

	public boolean getIsBroadcast() {
		return sp.getBoolean(ISBROADCAST, true);
	}

	// 设置--播放时间
	public void setBroadcastTime(String broadcastTime) {
		editor.putString(BROADCAST_TIME, broadcastTime);
		editor.commit();
	}

	public String getBroadcastTime() {
		return sp.getString(BROADCAST_TIME, "08:00");
	}

	// 设置--是否推送
	public void setIsPush(boolean isPush) {
		editor.putBoolean(ISPUSH, isPush);
		editor.commit();
	}

	public boolean getIsPush() {
		return sp.getBoolean(ISPUSH, true);
	}

	// 设置--推送时间
	public void setPushTime(String pushTime) {
		editor.putString(PUSH_TIME, pushTime);
		editor.commit();
	}

	public String getPushTime() {
		return sp.getString(PUSH_TIME, "08:00");
	}
	
	//关注的城市
	public Map<String,SimpleCity> getFocusCitys(){
		Map<String,SimpleCity> result=new HashMap<String,SimpleCity>();
		try{
			String temp=sp.getString(FOCUSCITYS, "");
			if(temp!=null&&temp!=""){
				result=new com.google.gson.Gson().fromJson(temp,new TypeToken<Map<String,SimpleCity>>() {
				}.getType());
			}
		}catch(Exception e){e.printStackTrace();}
		return result;
	}
	
	public void setFocusCitys(Map<String,SimpleCity> citys){
		try{
			String temp=new com.google.gson.Gson().toJson(citys, new TypeToken<Map<String,SimpleCity>>() {
				}.getType());
			if(temp!=null&&temp!=""){
				editor.putString(FOCUSCITYS, temp);
				editor.commit();
			}
		}catch(Exception e){e.printStackTrace();}
	}

	// city
	public void setCity(String city) {
		// TODO Auto-generated method stub
		editor.putString(CASH_CITY, city);
		editor.commit();
	}

	public String getCity() {
		return sp.getString(CASH_CITY, "");
	}

	// SimpleClimate
	public void setSimpleClimate(String climate) {
		editor.putString(SIMPLE_CLIMATE, climate);
		editor.commit();
	}

	public String getSimpleClimate() {
		return sp.getString(SIMPLE_CLIMATE, "N/A");
	}

	// SimpleTemp
	public void setSimpleTemp(String temp) {
		editor.putString(SIMPLE_TEMP, temp);
		editor.commit();
	}

	public String getSimpleTemp() {
		return sp.getString(SIMPLE_TEMP, "");
	}

	// timesamp
	public void setTimeSamp(long time) {
		editor.putLong(TIMESAMP, time);
		editor.commit();
	}

	public long getTimeSamp() {
		return sp.getLong(TIMESAMP, System.currentTimeMillis());
	}

	// time
	public void setTime(String time) {
		editor.putString(TIME, time);
		editor.commit();
	}

	public String getTime() {
		return sp.getString(TIME, "");
	}

	// 获取url的?的前一节，用来做缓存用
	private String GetUrlFore(String url) {
		String result = "";
		try {
			if (url != null && !url.equals("")) {
				String[] temps = url.split("\\?");
				if (temps != null && temps.length > 0) {
					result = temps[0];
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	// 设置请求的缓存
	public void setWeatherCache(String url, String data) {
		String tu = GetUrlFore(url);
		try {
			if (!tu.equals("")) {
				editor.putString(tu, data);
				editor.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 获取请求的缓存
	public String getWeatherCache(String url) {
		String result = "";
		try {
			String tu = GetUrlFore(url);
			result = sp.getString(tu, "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
